import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {BusinessStrategyService} from '../business-strategy.service';
import {NzMessageService} from 'ng-zorro-antd/message';

enum STATUS {
  '已上架' = 1,
  '已下架',
  '草稿'
}

@Component({
  selector: 'app-article-manage',
  templateUrl: './article-manage.component.html',
  styleUrls: ['./article-manage.component.scss']
})
export class ArticleManageComponent implements OnInit {
  query = {
    page: 1,
    pageSize: 10,
  };
  listData = {
    page: 1,
    total: 100,
    records: []
  };
  STATUS = STATUS;

  constructor(
    private router: Router,
    private bSService: BusinessStrategyService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.getTableData();
  }

  onPageIndexChange(index: number) {
    this.query.page = index;
    // this.getTableList();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    // this.getTableList();
  }


  getTableData() {
    this.bSService.getArticle(this.query).then((res: any) => {
      if (res.code === 0) {
        this.listData = res.data;
      } else {
        this.nzMessageService.error(res.message);
      }
    });
  }

  move(id: any, status: number) {
    this.bSService.moveArticle(id, status).then((res: any) => {
      if (res.code === 0) {
        this.getTableData();
      }
    });
  }

  changeStatus(data: any, status: number) {
    this.bSService.editArticle({
      id: data.id,
      status,
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getTableData();
      } else {
        this.nzMessageService.error(res.message);
      }
    });
  }

  deleteArticle(id: any) {
    this.bSService.deleteArticle(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.getTableData();
      }
    });
  }

  addArticle() {
    this.router.navigate(['/customer/articleAdd']);
  }

  edit(data: any) {
    this.router.navigate(['/customer/articleAdd'], {queryParams: {data: JSON.stringify(data)}});
  }
}
